/******************************************************************************
@file:  GymYogaRegistration.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
******************************************************************************/

package com.smartc.health;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.connection.ParsedLoginData;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class GymYogaRegistration extends Activity implements OnClickListener {

	private static final String TAG = "GymYogaRegistration";
	private static final int REG_STATUS = 0x01;
	private final int PROGRESS_DLG = 0x02;
	private EditText mAssociateId = null;
	private EditText mName = null;
	private EditText mLocation = null;
	private EditText mContact_nu = null;
	private Spinner mSlot = null;
	private Spinner mGender = null;

	private Button mRegister = null;

	private String mIdType = null;
	private String mId = null;
	private String mTotalSeatCapacity = null;
	private String mStrSlot = "Morning";
	private String mStrGender = "Male";
	private String[] mSlot_Arr = new String[2];
	private String[] mGender_Arr = new String[2];
	private HttpConnection mConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gym_yoga_registration);
		mConnection = new HttpConnection();
		mConnection.AddHandler(mResponse);

		mIdType = getIntent().getStringExtra("idType");
		mId = getIntent().getStringExtra("id");
		mTotalSeatCapacity = getIntent().getStringExtra("nrTotalSeatCapacity");

		mAssociateId = (EditText) findViewById(R.id.gy_edit_associateid);
		mName = (EditText) findViewById(R.id.gy_edit_name);
		mLocation = (EditText) findViewById(R.id.gy_edit_location);
		mContact_nu = (EditText) findViewById(R.id.gy_edit_contact);
		mSlot = (Spinner) findViewById(R.id.gy_edit_slot);
		mGender = (Spinner) findViewById(R.id.gy_edit_gender);

		mRegister = (Button) findViewById(R.id.gy_submit_button);
		mRegister.setOnClickListener(this);
		mSlot_Arr[0] = "Morning";
		mSlot_Arr[1] = "Evening";

		ArrayAdapter Adapter = new ArrayAdapter(GymYogaRegistration.this,
				R.layout.simple_spinner_item, mSlot_Arr);
		mSlot.setAdapter(Adapter);
		mSlot.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() position:" + position);
				mStrSlot = mSlot_Arr[position];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});

		mGender_Arr[0] = "Male";
		mGender_Arr[1] = "Female";
		Adapter = new ArrayAdapter(GymYogaRegistration.this,
				R.layout.simple_spinner_item, mGender_Arr);
		mGender.setAdapter(Adapter);
		mGender.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "onItemSelected() position:" + position);
				mStrGender = mGender_Arr[position];
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	@Override
	public void onClick(View view) {
		if (view.getId() == R.id.gy_submit_button) {
			if(validateInput() == false)
				return;
			
			String domain = getResources().getString(R.string.domain);
			String query = getResources().getString(
					R.string.url_health_care_gym_yoga_registration);
			String url = domain + query;
			if (AccountLogin.DEBUG)
				Log.i(TAG, "onStart url:" + url);
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mStrSlot:" + mStrSlot.substring(0, 1));
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mStrGender:" + mStrGender.substring(0, 1));
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mIdType:" + mIdType);
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart id:" + mId);
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mTotalSeatCapacity:" + mTotalSeatCapacity);
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mStrSlot:" + mStrSlot);
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mAssociateId:" + mAssociateId.getText()
					.toString().trim());
			if (AccountLogin.DEBUG)Log.i(TAG, "onStart mName:" + mName.getText().toString().trim());
			if (AccountLogin.DEBUG) Log.i(TAG, "onStart mLocation:" + mLocation.getText().toString()
					.trim());

			mConnection.setUrl(url);
			mConnection.clearParam();
			mConnection.AddParam("idType", mIdType);
			mConnection.AddParam("id", mId);
			mConnection.AddParam("idUser", ParsedLoginData.mIdUser);
			mConnection.AddParam("nrTotalSeatCapacity", mTotalSeatCapacity);
			mConnection.AddParam("idSlot", mStrSlot.substring(0, 1));
			mConnection.AddParam("idAssociate", mAssociateId.getText()
					.toString().trim());
			mConnection.AddParam("nmName", mName.getText().toString().trim());
			mConnection.AddParam("nmLocation", mLocation.getText().toString()
					.trim());
			mConnection.AddParam("inGender", mStrGender.substring(0, 1));
			mConnection.Execute();
			showDialog(PROGRESS_DLG);
		}
	}
	
	private boolean validateInput(){
		boolean valid = true;
		if(mAssociateId.getText().toString().trim().length() <=0   ||
				mName.getText().toString().trim().length() <=0     ||
				mLocation.getText().toString().trim().length() <=0 ||
				mContact_nu.getText().toString().trim().length() <=0
				){
			valid = false;
			Toast.makeText(this, "Enter All Input fields", Toast.LENGTH_SHORT)
					.show();
		}else{
			if (mContact_nu.getText().toString().trim().length()<10 || 
					mContact_nu.getText().toString().trim().length()>13/* || 
					nmContact.matches(numStr)==false*/) {
				valid = false;
				Toast.makeText(this, "Enter valid Mobile Number", Toast.LENGTH_SHORT).show();
			}
		}
		return valid;
	}

	public class ResponseHandler extends DefaultHandler {
		private boolean message = false;
		public String mMessage = null;

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.message) {
				mMessage = new String(ch, start, length);
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("Messages")) {
				Message myMessage = new Message();
				myMessage.obj = "MESSAGE";
				handler.sendMessage(myMessage);
			}else if (localName.equals("message")) {
				this.message = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("message")) {
				this.message = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			if (loginmsg != null && loginmsg.equals("MESSAGE")) {
				removeDialog(PROGRESS_DLG);
				showDialog(REG_STATUS);
			}
		}
	};
	
	@Override
	protected Dialog onCreateDialog(int id, Bundle args) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onCreateDialog id" + id);
		switch (id) {
		case REG_STATUS:
			return new AlertDialog.Builder(GymYogaRegistration.this).setMessage(
					mResponse.mMessage).setPositiveButton("OK", null).create();
			
		case PROGRESS_DLG:
			ProgressDialog mDialog = new ProgressDialog(this);
			mDialog.setMessage("Request In progress....");
			mDialog.setIndeterminate(true);
			mDialog.setCancelable(true);
			return mDialog;
		default:
			return super.onCreateDialog(id, args);
		}
	}
}
